package com.pin.zheng.xingmulibrary.base;

import android.app.ProgressDialog;
import android.view.View;
import android.widget.Toast;

import androidx.lifecycle.Lifecycle;


import com.pin.zheng.xingmulibrary.view.IBaseView;

import autodispose2.AutoDispose;
import autodispose2.AutoDisposeConverter;
import autodispose2.androidx.lifecycle.AndroidLifecycleScopeProvider;


public abstract class BaseMvpFragment<T extends BasePresenter> extends BaseFragment implements IBaseView {

    protected T mPresenter;
    protected ProgressDialog progressDialog = null;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.detachView();
            mPresenter = null;
        }
        super.onDestroyView();
    }

    @Override
    protected void initTitleLayout(View view) {
    }

    /**
     * 绑定生命周期 防止MVP内存泄漏
     *
     * @param <T>
     * @return
     */
    @Override
    public <T> AutoDisposeConverter<T> bindAutoDispose() {
        return AutoDispose.autoDisposable(AndroidLifecycleScopeProvider
                .from(this, Lifecycle.Event.ON_DESTROY));
    }

    @Override
    public void showLoading() {
        if (progressDialog == null || ! progressDialog.isShowing()){
            progressDialog = ProgressDialog.show(getActivity(), "请稍等...", "获取数据中...", true);
        }

    }

    @Override
    public void hideLoading() {

        progressDialog.dismiss();
    }

    @Override
    public void onError(String errMessage) {
        Toast.makeText(getActivity(), errMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void initConfiguration() {

    }

    @Override
    protected void loadData() {
        if (mPresenter == null) {
            initPresenter();
        }

    }

    protected void initPresenter() {

    }
}
